const Error = require('./error')
const storage = require('./storage.js')
const request = require("./request.js")
const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isArray
} = require('./dataType.js')

const yachtClub = class yachtClub {

    constructor() {}

    get(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.YACHT_CLUB.GET_YACHT_CLUB.url,
                MS._config.parameters.YACHT_CLUB.GET_YACHT_CLUB.method, params).then(res => {
                    resolve(res.data)
            }).catch(err =>{
                reject(err)
            })
        })
    }

    query(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if(!isUndefined(options)) {
            if(!isUndefined(options.name) && isString(options.name)) {
                params.name = options.name;
            }
            if(!isUndefined(options.address) && isString(options.address)) {
                params.address = options.address;
            }
            if(!isUndefined(options.longitude) && isNumber(options.longitude)) {
                params.longitude = options.longitude;
            }
            if(!isUndefined(options.latitude) && isNumber(options.options.latitude)) {
                params.latitude = options.latitude;
            }
            if(!isUndefined(options.areaCode) && isString(options.areaCode)) {
                params.areaCode = options.areaCode;
            }
            if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum;
            }
            if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.YACHT_CLUB.LOAD_YACHT_CLUB.url,
                MS._config.parameters.YACHT_CLUB.LOAD_YACHT_CLUB.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    save(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.id) && isString(options.id)) {
                params.id = options.id;
            }
            if(!isUndefined(options.name) && isString(options.name)) {
                params.name = options.name;
            }
            if(!isUndefined(options.keeperName) && isString(options.keeperName)) {
                params.keeperName = options.keeperName;
            }
            if(!isUndefined(options.phone) && isString(options.phone)) {
                params.phone = options.phone;
            }
            if(!isUndefined(options.telephone) && isString(options.telephone)) {
                params.telephone = options.telephone;
            }
            if(!isUndefined(options.website) && isString(options.website)) {
                params.website = options.website;
            }
            if(!isUndefined(options.email) && isString(options.email)) {
                params.email = options.email;
            }
            if(!isUndefined(options.qrCode) && isString(options.qrCode)) {
                params.qrCode = options.qrCode;
            }
            if(!isUndefined(options.yachtNum) && isNumber(options.yachtNum)) {
                params.yachtNum = options.yachtNum;
            }
            if(!isUndefined(options.berthNum) && isNumber(options.berthNum)) {
                params.berthNum = options.berthNum;
            }
            if(!isUndefined(options.berthUseNum) && isNumber(options.berthUseNum)) {
                params.berthUseNum = options.berthUseNum;
            }
            if(!isUndefined(options.warehouseNum) && isNumber(options.warehouseNum)) {
                params.warehouseNum = options.warehouseNum;
            }
            if(!isUndefined(options.warehouseUseNum) && isNumber(options.warehouseUseNum)) {
                params.warehouseUseNum = options.warehouseUseNum;
            }
            if(!isUndefined(options.address) && isString(options.address)) {
                params.address = options.address;
            }
            if(!isUndefined(options.areaCode) && isString(options.areaCode)) {
                params.areaCode = options.areaCode;
            }
            if(!isUndefined(options.longitude) && isNumber(options.longitude)) {
                params.longitude = options.longitude;
            }
            if(!isUndefined(options.latitude) && isNumber(options.latitude)) {
                params.latitude = options.latitude;
            }
            if(!isUndefined(options.images) && isArray(options.images)) {
                params.images = options.images;
            }
        }
        if (!isUndefined(options.id) && isString(options.id)) {
            this.update(params)
        } else {
            this.create(params)
        }
    }

    create(params) {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.YACHT_CLUB.CREATE_YACHT_CLUB.url,
                MS._config.parameters.YACHT_CLUB.CREATE_YACHT_CLUB.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    update(params) {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.YACHT_CLUB.UPDATE_YACHT_CLUB.url,
                MS._config.parameters.YACHT_CLUB.UPDATE_YACHT_CLUB.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    remove(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.YACHT_CLUB.REMOVE_YACHT_CLUB.url,
                MS._config.parameters.YACHT_CLUB.REMOVE_YACHT_CLUB.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = yachtClub